package cn.zuo.view;

import cn.zuo.pojo.Message;
import cn.zuo.pojo.User;
import cn.zuo.utils.ChatPool;
import cn.zuo.utils.PPKeyFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.net.Socket;
import java.util.*;

/**
 * @Author 白白旧维
 * @Date 2021/7/7 11:13
 */
public class UserMain {
    private Map<String, BigInteger> key;//自己的密钥
//    private String seed;//AES密钥

    private JPanel mjp;

    public JPanel getMjp() {
        return mjp;
    }

    private JLabel nameLabel;
    private JList list1;
    private Socket s;
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Socket getS() {
        return s;
    }

    public void setS(Socket s) {
        this.s = s;
    }

    public UserMain(String name,Socket s,Vector<String> vector,Map<String,BigInteger> key) {
        this.name = name;
        this.s = s;
        this.key = key;
        nameLabel.setText("你好，"+name+"!");


        list1.setListData(vector);

        list1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount()==2){
//                    System.out.println(list1.getSelectedValue().toString());
                    JFrame jFrame = new JFrame();

                    String sender = name;
                    String receiver = list1.getSelectedValue().toString();

                    ChatJFrame chatJFrame = new ChatJFrame(sender,receiver,s,key);
                    chatJFrame.setS(s);
                    ChatPool.setChat(sender+"_"+receiver,chatJFrame);
                    jFrame.setContentPane(chatJFrame.getMjp());
                    jFrame.setVisible(true);
                    jFrame.pack();
                    jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    jFrame.setLocation(600,500);
                    jFrame.setTitle(sender+"和"+receiver+"聊天页面  <©2021 白白旧维>");
                    jFrame.addWindowListener(new WindowListener() {
                        @Override
                        public void windowOpened(WindowEvent e) {
                        }

                        @Override
                        public void windowClosing(WindowEvent e) {
                            ChatPool.removeChat(sender+"_"+receiver);
                        }

                        @Override
                        public void windowClosed(WindowEvent e) {

                        }

                        @Override
                        public void windowIconified(WindowEvent e) {

                        }

                        @Override
                        public void windowDeiconified(WindowEvent e) {

                        }

                        @Override
                        public void windowActivated(WindowEvent e) {

                        }

                        @Override
                        public void windowDeactivated(WindowEvent e) {

                        }
                    });

                }

            }
        });



    }

}
